﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace MachineLearning {
	class FindS : ConceptLearner {
		public FindS(string fileName) : base(fileName) {
			//sets the most specific hypothesis
			XName name = "row";
			_currentHypothesis = new XElement(name);
			XAttribute newAttribute = null;
			_attributeList = new List<XName>();
			foreach (XAttribute a in _dataSet.SampleRowElement.Attributes()) {
				newAttribute = new XAttribute(a.Name, "");
				_attributeList.Add(a.Name);
				_currentHypothesis.Add(newAttribute);
			}
			_targetConcept = _attributeList[_attributeList.Count - 1];
			findMaximallySpecificHypothesis();
		}

		private void findMaximallySpecificHypothesis() {
			foreach (XElement e in _trainingData) {
				if ((string)e.Attribute(_targetConcept) == "Yes")
					compareToCurrentHypothesis(e);
			}
		}

		private void compareToCurrentHypothesis(XElement e) {
			if ((string)_currentHypothesis.Attribute(_targetConcept) == "")
				_currentHypothesis = new XElement(e);
			else {
				XName a = null;
				for (int i = 0; i < _attributeList.Count - 1; i++) {
					a = _attributeList[i];
					if ((string)e.Attribute(a) != (string)_currentHypothesis.Attribute(a))
						_currentHypothesis.SetAttributeValue(a, QUESTION);
				}
			}
		}

	}
}
